package com.example.demo.controller;

import com.example.demo.services.PowerService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/check")
public class PowerController {
    private final PowerService powerService;

    public PowerController(PowerService powerService) {
        this.powerService = powerService;
    }


    @PostMapping("/checkPower")
    public boolean checkPower(@RequestParam String userID, @RequestParam String moduleID, @RequestParam String subPower) {
        //System.out.println("userID: " + userID + ", moduleID: " + moduleID + ", subPower: " + subPower);
        return powerService.hasPowerByModuleID(userID, moduleID, subPower);
    }

    @GetMapping("/allPower")
    public List<Object> checkAllPower(@RequestParam String userID) {
        //System.out.println("2: userID = " + userID);

        return powerService.hasPowerAll(userID);
    }
    @PostMapping("/power")
    public String checkOnlyPower(@RequestBody Map<String, String> payload) {
        String userID = payload.get("userID");
        String moduleID = payload.get("moduleID");
        //System.out.println("2: userID = " + userID + " 3：moduleID:" + moduleID);
        String originData = powerService.hasPower(userID, moduleID);
        String newData = originData.replaceAll("\\]\\[", "\",\"");
        newData = newData.replaceAll("\\[","\"");
        newData = newData.replaceAll("\\]","\"");
        newData = "[" + newData + "]";
        //System.out.println(newData);
        return newData;
    }
}
